package hot100;

import org.junit.Test;

import java.util.List;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/9/13 15:41
 */
public class lc234回文链表 {


    public boolean solve(ListNode head){

        ListNode fast = head,slow = head;
        while(fast !=null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        ListNode pre = null , cur = slow;
        while(cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp; // 反转链表 不要写错!
        }
        cur = head;
        while(pre !=null){
            if(cur.val != pre.val){
                return false;
            }
            pre = pre.next;
            cur = cur.next;
        }

        return true;
    }


    @Test
    public void test(){
        ListNode head = new ListNode(1);
        ListNode head1 = new ListNode(2);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(1);
        head.next = head1;
        head1.next = head2;
        head2.next = head3;
        boolean solve = solve(head);
        System.out.println(solve);
    }


    class ListNode {

        int val;
       ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
